const dayjs = require('dayjs')
const fs=require('fs')
const path=require('path')
const NewsService =require('../../services/admin/NewsService')
const NewsController={
    AddNews:async (req,res)=>{
          const data=   Object.values(req.body)
         data.push('/newsavator/'+req.file.filename)
        let now = dayjs()
        data.push(now.format('YYYY-MM-DD HH:mm:ss'))
       const result = await NewsService.AddNews(data)
       if(result.code===0){
        res.send({
            code:0,
            msg:'添加失败'
        })
       }
       else 
       res.send({
             code:200,
             msg:'添加成功'
         })
    },
    GetAllNews:async(req,res)=>{
        const result = await NewsService.GetAllNews()
        if(result.code===0){
         res.send({
             code:0,
             msg:'添加失败'
         })
        }
        else 
        res.send({
              code:200,
              msg:'查看成功',
              result:result
          })
    },
    isPublish:async(req,res)=>{
        console.log(req.body);
        const result =await NewsService.isPublish(req.body)
        if(result.code===0){
            res.send({
                code:0,
                msg:'发布失败'
            })
           }
           else 
           res.send({
                 code:200,
                 msg:'发布成功',
                
             })
    },
    Newdelete:async(req,res)=>{
        console.log(req.body);
        const result =await NewsService.Newdelete(req.body.id)
        if(result.code===0){
            res.send({
                code:0,
                msg:'删除失败'
            })
           }
           else {
            fs.unlink(path.resolve('public'+req.body.cover),(err)=>{
                if(err)
                        console.log(err);
                  })
            res.send({
                code:200,
                msg:'删除成功',
               
            })
           }
         
             
    },
    NewEdit:async(req,res)=>{
        let now = dayjs()
        if(req.file){
            fs.unlink(path.resolve('public'+req.body.cover),(err)=>{
                if(err)
                        console.log(err);
                  })
                const params = [req.body.title,req.body.content,req.body.category,req.body.isPublish,now.format('YYYY-MM-DD HH:mm:ss'),'/newsavator/'+req.file.filename,req.body.id]
                const sql = 'update news set title=?,content=?,category=?,isPublish=?,editTime=?,cover=? where id =?'
                const result = await NewsService.NewEdit(sql,params)
                if(result.code===0){
                   res.send({
                       code:0,
                       msg:'修改失败'
                   })
                  }
                  else 
                  res.send({
                        code:200,
                        msg:'修改成功',
                       
                    })
                }
                else {
                    const params = [req.body.title,req.body.content,req.body.category,req.body.isPublish,now.format('YYYY-MM-DD HH:mm:ss'),req.body.id]
                    const sql = 'update news set title=?,content=?,category=?,isPublish=?,editTime=? where id =?'
                    const result = await NewsService.NewEdit(sql,params)
                    if(result.code===0){
                        res.send({
                            code:0,
                            msg:'修改失败'
                        })
                    }
                    else 
                    res.send({
                            code:200,
                            msg:'修改成功',
                            
                        })
                }
            }
        }


module.exports = NewsController